package com.rabbit.rabbitmqspringboot.config;



import com.rabbit.rabbitmqspringboot.constant.Constants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
    // 声明队列

    // 工作模式
    @Bean("workqueue")
    public Queue workqueue(){
        return QueueBuilder.durable(Constants.WORK_QUEUE).build();
    }

    // 订阅模式
    @Bean("fanoutQueue1")
    public Queue fanoutQueue1(){
        return QueueBuilder.durable(Constants.FANOUT_QUEUE1).build();
    }
    @Bean("fanoutQueue2")
    public Queue fanoutQueue2(){
        return QueueBuilder.durable(Constants.FANOUT_QUEUE2).build();
    }
    @Bean("fanoutExchange")
    public FanoutExchange fanoutExchange(){
        return ExchangeBuilder.fanoutExchange(Constants.FANOUT_EXCHANGE).durable(true).build();
    }

    //交换机与队列的绑定
    @Bean("fanoutBinding1")
    public Binding fanoutBinding1(@Qualifier("fanoutExchange") FanoutExchange exchange,
                                  @Qualifier("fanoutQueue1") Queue queue){
        return BindingBuilder.bind(queue).to(exchange);
    }
    @Bean("fanoutBinding2")
    public Binding fanoutBinding2(@Qualifier("fanoutExchange") FanoutExchange exchange,
                                  @Qualifier("fanoutQueue2") Queue queue){
        return BindingBuilder.bind(queue).to(exchange);
    }



    // 路由模式 ： direct
    @Bean("directQueue1")
    public Queue directQueue1(){
        return QueueBuilder.durable(Constants.DIRECT_QUEUE1).build();
    }
    @Bean("directQueue2")
    public Queue directQueue2(){
        return QueueBuilder.durable(Constants.DIRECT_QUEUE2).build();
    }
    @Bean("directExchange")
    public DirectExchange directExchange(){
        return ExchangeBuilder.directExchange(Constants.DIRECT_EXCHANGE).durable(true).build();
    }
    @Bean("directBinding1")
    public Binding directBinding1(@Qualifier("directExchange") DirectExchange exchange, @Qualifier("directQueue1") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("a");
    }
    @Bean("directBinding21")
    public Binding directBinding21(@Qualifier("directExchange") DirectExchange exchange, @Qualifier("directQueue2") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("a");
    }
    @Bean("directBinding22")
    public Binding directBinding22(@Qualifier("directExchange") DirectExchange exchange, @Qualifier("directQueue2") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("b");
    }
    @Bean("directBinding23")
    public Binding directBinding23(@Qualifier("directExchange") DirectExchange exchange, @Qualifier("directQueue2") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("c");
    }



    // topic 通配符模式
    @Bean("topicQueue1")
    public Queue topicQueue1(){
        return QueueBuilder.durable(Constants.TOPIC_QUEUE1).build();
    }
    @Bean("topicQueue2")
    public Queue topicQueue2(){
        return QueueBuilder.durable(Constants.TOPIC_QUEUE2).build();
    }
    @Bean("topicExchange")
    public TopicExchange topicExchange(){
        return ExchangeBuilder.topicExchange(Constants.TOPIC_EXCHANGE).durable(true).build();
    }

    @Bean("topicBinding1")
    public Binding topicBinding1(@Qualifier("topicQueue1") Queue queue,@Qualifier("topicExchange") TopicExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("*.a.*");
    }
    @Bean("topicBinding21")
    public Binding topicBinding21(@Qualifier("topicQueue2") Queue queue,@Qualifier("topicExchange") TopicExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("*.a.*");
    }
    @Bean("topicBinding22")
    public Binding topicBinding22(@Qualifier("topicQueue2") Queue queue,@Qualifier("topicExchange") TopicExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("*.b.*");
    }
    @Bean("topicBinding23")
    public Binding topicBinding23(@Qualifier("topicQueue2") Queue queue,@Qualifier("topicExchange") TopicExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("c.#");
    }




}
